// 封装axios
import store from '@/store';
import axios from 'axios'
import { showNotify, showLoadingToast,  closeToast } from 'vant';
// 使用create创建axios实例
const Service = axios.create({
    timeout: 20000,
    baseURL: 'http://43.139.248.149:9082/api/v1',
    headers: {
        "Accept": "*/*",
        "Content-Type": "multipart/form-data; boundary=<calculated when request is sent>",
        "token": store.state.Token.token
    }
})


// 请求拦截-增加loading，对请求做统一护理
Service.interceptors.request.use(config => {
    showLoadingToast({
        message: '加载中...',
        duration:0,
        forbidClick: true,
    });
    return config
})

// 响应拦截-对返回值做统一处理
Service.interceptors.response.use(response => {
    closeToast();
    const data = response
    if (data.status != 200) {
        showNotify({ type: 'warning', message: data.message || '服务器出错' });
        return data
    }
    return data
}, error => {
    closeToast();
    showNotify({ type: 'warning', message: data.message || '服务器出错' });
})
// get请求
export const get = config => {
    return Service({
        ...config,
        method: 'get',
        data: config.data
    })
}
// post请求
export const post = config => {
    return Service({
        ...config,
        method: 'post',
        data: config.data
    })
}
// delete请求
export const del = config => {
    return Service({
        ...config,
        method: 'delete',
        data: config.data
    })
}
// put请求
export const put = config => {
    return Service({
        ...config,
        method: 'put',
        data: config.data
    })
}